package com.atguigu.imperial.court.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;


import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource dataSource;
    private static ThreadLocal<Connection> threadLocal=new ThreadLocal<>();
    static {
        ClassLoader classLoader = JDBCUtils.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("jdbc.properties");
        Properties properties=new Properties();
        try {
            properties.load(inputStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection(){
        Connection connection=null;
        try{
            connection = threadLocal.get();
            if(connection==null){
                connection=dataSource.getConnection();
                threadLocal.set(connection);
            }
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return connection;
    }
    public static void releaseConnection(Connection connection){
        if(connection!=null){
            try{
                connection.close();
                threadLocal.remove();
            }catch (SQLException e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
}
